﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using INF1010_Chat;

namespace ClientChat
{
    public partial class frmClient : Form
    {
        private delegate void RefreshUserListDelegate();
        public RefreshUserListDelegate refreshUserList2;
        private bool isManagingSelection;

        private ClientChat clientChat;

        public frmClient()
        {
            InitializeComponent();
            clientChat = ClientChat.GetClientChat();
            refreshUserList2 = new frmClient.RefreshUserListDelegate(RefreshUserList);
            clientChat.RefreshList += new ClientChat.ReceivedRefreshUsersEventHandler(clientChat_RefreshList);
            clientChat.UserMessageReceive += new ClientChat.UserMessageReceiveEventHandler(clientChat_UserMessageReceive);
        }

        void clientChat_UserMessageReceive(object sender, EventArgs args)
        {
            string newMessage = clientChat.DequeueUserMessage();

            txtNewMessage.Text += newMessage + Environment.NewLine;
        }

        private void RefreshUserList()
        {
            lbUsers.Items.Clear();
            foreach (User user in clientChat.Users)
            {
                lbUsers.Items.Add(user);
            }
        }

        void clientChat_RefreshList(object sender, EventArgs args)
        {
            if (lbUsers.InvokeRequired)
            {
                lbUsers.Invoke(refreshUserList2);
            }
        }

        private void frmClient_Load(object sender, EventArgs e)
        {

        }

        private void btnConnect_Click(object sender, EventArgs e)
        {
            if (!String.IsNullOrEmpty(txtUserName.Text))
            {
                if (!clientChat.IsConnected)
                {
                    clientChat.SendConnect(txtUserName.Text);
                    btnConnect.Text = "Se déconnecter";
                }
                else
                {
                    clientChat.SendDisconnect();
                    btnConnect.Text = "Se connecter";
                }
            }
        }

        private void btnRefreshUserList_Click(object sender, EventArgs e)
        {
            clientChat.SendRefreshUserList();
        }

        private void lbUsers_SelectedValueChanged(object sender, EventArgs e)
        {
            if (!isManagingSelection)
            {
                isManagingSelection = true;

                lbGroups.ClearSelected();

                isManagingSelection = false;
            }
        }

        private void lbGroups_SelectedValueChanged(object sender, EventArgs e)
        {
            if (!isManagingSelection)
            {
                isManagingSelection = true;

                lbUsers.ClearSelected();

                isManagingSelection = false;
            }
        }

        private void btnJoinGroup_Click(object sender, EventArgs e)
        {
            if (!String.IsNullOrEmpty(txtJoinGroup.Text))
            {
                clientChat.SendJoinGroup(txtJoinGroup.Text);
            }
            // Todo rafraichir liste de groupes
        }

        private void btnQuitterGroup_Click(object sender, EventArgs e)
        {
            if (lbGroups.SelectedItem != null)
            {
                clientChat.SendLeaveGroup(lbGroups.SelectedItem.ToString());
            }
            //Todo rafraichir liste de groupes
        }

        private void btnSendMessage_Click(object sender, EventArgs e)
        {

        }

    }
}
